package com.wehealth.three.lib_common.ui.component

import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.os.Bundle
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.wehealth.three.lib_common.R
import com.wehealth.three.lib_common.utils.StatusBarUtil
import com.wehealth.three.lib_common.widget.OldLoadingDialog

open abstract class BaseActivity<T : ViewDataBinding>(
    private val isFitStatus: Boolean = true,
    private val barColor: Int = Color.TRANSPARENT,
    private val darkContent: Boolean = true,
    private val translucent: Boolean = false

) :
    AppCompatActivity() {
      lateinit var loaDialog: OldLoadingDialog
    lateinit var dataBinding: T
    lateinit var manager: InputMethodManager
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        loaDialog = OldLoadingDialog(this)
        dataBinding = DataBindingUtil.setContentView(this, getLayoutId())
        manager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        if (isFitStatus) {
//            fitNavigationBar(barColor, darkContent, translucent)
            fitStatusBar(barColor, darkContent, translucent)
        }
        initData()


    }

    override fun onResume() {
        super.onResume()
        if (isHideSoftKeyBoard()) {
            hideKeyboard()
        }
    }


    abstract fun getLayoutId(): Int


    private fun isHideSoftKeyBoard(isHide: Boolean = true): Boolean {
        return isHide
    }


    abstract fun initData()

    /***
     * 适配状态栏
     */
    private fun fitStatusBar(
        color: Int = Color.TRANSPARENT,
        darkContent: Boolean,
        translucent: Boolean
    ) {
        StatusBarUtil.setStatusBar(this, darkContent, color, translucent)
    }

    /***
     * 适配导航栏
     */
    private fun fitNavigationBar(
        color: Int = Color.TRANSPARENT,
        darkContent: Boolean = true,
        translucent: Boolean = false
    ) {
        StatusBarUtil.setNavigationBar(this, darkContent, color, translucent)

    }

    /**
     * 隐藏软键盘
     */
    private fun hideKeyboard() {
        if (window.attributes.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (currentFocus != null) manager.hideSoftInputFromWindow(
                currentFocus!!.windowToken,
                InputMethodManager.HIDE_NOT_ALWAYS
            )
        }
    }


    /**对话框 点击确定结束 */
    protected open fun noticeFinishDialog(message: String?) {
        if (isFinishing) {
            return
        }
        val builder = AlertDialog.Builder(this)
        builder.setTitle(getResources().getString(R.string.friend_notify))
        builder.setMessage(message)
        builder.setCancelable(false)
        builder.setPositiveButton("确定") { dialog: DialogInterface, which: Int ->
            dialog.dismiss()
            finish()
        }
        builder.show()
    }  /**对话框 点击确定结束 */
    protected open fun noticeDialog(message: String?,action:()->Unit) {
        if (isFinishing) {
            return
        }
        val builder = AlertDialog.Builder(this)
        builder.setTitle(getResources().getString(R.string.friend_notify))
        builder.setMessage(message)
        builder.setCancelable(false)
        builder.setPositiveButton("确定") { dialog: DialogInterface, which: Int ->
            dialog.dismiss()
             action()
        }
        builder.show()
    }

}